//
//  PicPickerCollectionView.swift
//  JIMWeiBo
//
//  Created by 陈恒均 on 2018/2/23.
//  Copyright © 2018年 陈恒均. All rights reserved.
//

import UIKit
private let edgeMargin : CGFloat = 15
private let picPickerCell = "picPickerCell"
class PicPickerCollectionView: UICollectionView {
    
    
    var images : [UIImage] = [UIImage](){
        didSet{
            reloadData()
        }
    }
    
    override func awakeFromNib() {
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemWH = (UIScreen.main.bounds.width - 4 * edgeMargin) / 3
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 15
        self.backgroundColor = UIColor.lightGray
   
        
        register(UINib(nibName: "PicPickerCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: picPickerCell)
        
        dataSource = self
        
        contentInset = UIEdgeInsets(top: edgeMargin, left: edgeMargin, bottom: 0, right: edgeMargin)
        
    }

}

extension PicPickerCollectionView : UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picPickerCell, for: indexPath) as! PicPickerCollectionViewCell
        cell.backgroundColor = UIColor.purple
        cell.image = indexPath.item <= images.count - 1 ? images[indexPath.item] : nil
        return cell
        
    }
}
